﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved All Rights Reserved All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website<http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FinPlusAssembler
{
    public static class ArrayExtension
    {
        public static string StringFrom1d(this object[] value, char delimiter = '%', string brackets = null)
        {
            if (value == null)
                return "";

            var str = string.Join(delimiter.ToString(), value);

            if (brackets != null)
                str = string.Format("{0}{1}{2}", brackets.First(), str, brackets.Last());

            return str;
        }

        public static string StringFrom2d(this object[,] value, char rowDelimiter = '%', char colDelimiter = '$')
        {
            try
            {
                //TODO clean all str parsing use string builder reg ex etc

                if (value == null) return "";

                StringBuilder sb = new StringBuilder();
                int rows = value.GetUpperBound(0);
                int cols = value.GetUpperBound(1);

                for (int i = 0; i <= rows; i++)
                {
                    for (int j = 0; j <= cols; j++)
                    {
                        sb.Append(value[i, j]);
                        if (i == rows && j == cols) continue;
                        sb.Append(j == cols ? rowDelimiter : colDelimiter);
                    }
                }
                return sb.ToString();
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        public static object To2d(this object[] value)
        {
            var r = new object[1, value.Length];

            var i = 0;
            foreach (var o in value)
            {
                r[0, i] = value[i];
                i++;
            }
            return r;
        }

        public static object Transpose(this object[] value)
        {
            var r = new object[value.Length, 1];

            var i = 0;
            foreach (var o in value)
            {
                r[i, 0] = value[i];
                i++;
            }
            return r;
        }

        public static object[,] Append(this object[,] value1, object[,] value2)
        {
            if (value1 == null)
                return value2;
            if (value2 == null)
                return value1;

            var l1 = value1.GetUpperBound(0) + 1;
            var w1 = value1.GetUpperBound(1) + 1;
            var l2 = value2.GetUpperBound(0) + 1;
            var w2 = value2.GetUpperBound(1) + 1;
            var l = l1 + l2;

            var res = new object[l, w1];

            for (int i = 0; i < l1; i++)
                for (int j = 0; j < w1; j++)
                    res[i, j] = value1[i, j];

            for (int i = l1; i < l; i++)
                for (int j = 0; j < w2; j++)
                    res[i, j] = value2[i - l1, j];

            return res;
        }

        public static IDictionary<string, object> ToDict(this object[,] value)
        {
            var res = new Dictionary<string, object>();

            if (value == null)
                return null;

            for (var i = 0; i <= value.GetUpperBound(0); i++)
                res[value[i, 0].ToString()] = value[i, 1];

            return res;
        }
    }
}
